//
//  API.swift
//  GHMoyaNetWorkTest
//
//  Created by Guanghui Liao on 3/30/18.
//  Copyright © 2018 liaoworking. All rights reserved.
//https://github.com/Moya/Moya/blob/master/docs_CN/Examples/Basic.md

import Foundation
import Moya

enum API{
    case productListPage(parameters: [String:Any])
    //发送短消息
    case sendSMS(parameters: [String:Any])
    case productDetail(parameters: [String:Any])
    case buyingProduct(parameters: [String:Any])
    //服务列表
    case serviceList(parameters: [String:Any])
    case serviceDetail(parameters: [String:Any])
    case buyingService(parameters: [String:Any])
    case storeList(parameters: [String:Any])
    case storeDetail(parameters: [String:Any])
    case uploadHeadImage(parameters: [String:Any])
    case dealHistory(parameters: [String:Any])
    case couponList(parameters: [String:Any])
    case modifyPwd(parameters: [String:Any])
    case hadboughtService(parameters: [String:Any])
    case updateUserInfo(parameters: [String:Any])
    case commentService(parameters: [String:Any])
    case skinFile(parameters: [String:Any])
    //美容轨迹
    case usedHistory(parameters: [String:Any])
    case getUserInfo(parameters: [String:Any])
    case loginByPwd(parameters: [String:Any])
    case loginByMsg(parameters: [String:Any])
    case getMsg(parameters: [String:Any])
    case getImgVerify(parameters: [String:Any])
    case logOut(parameters: [String:Any])
    //预约时候的服务列表
    case serviceListOfAppoint(parameters: [String:Any])
    case technicianList(parameters: [String:Any])
    case appointingService(parameters: [String:Any])
    case getDictDesc(parameters: [String:Any])
    case getDictList(parameters: [String:Any])
    case wechatLogin(parameters: [String:Any])
    case getTicket(parameters: [String:Any])
    case checkResult(parameters: [String:Any])
    case updateAPi(parameters: [String:Any])
    case register(email: String, password: String)
    case easyRequset
    case fortest
}

extension API:TargetType{
    var baseURL: URL {
        switch self {
        case .updateAPi:
            return URL.init(string:"http://h5.quzhaobei.com:6831/ZB-ESB/http/do.jhtml?router=")!
        case .easyRequset:
            return URL.init(string:(Moya_baseURL)+"")!
        case .fortest:
            return URL.init(string: "http://static.youshikoudai.com/")!
        default:
            return URL.init(string:"http://h5.quzhaobei.com:6831/ZB-ESB/http/do.jhtml?router=")!
        }
    }
    
    var path: String {
        switch self {
        case .productListPage://产品列表
            return "productService.productListPage"
        case .register://注册
            return "register"
        case .easyRequset:
            return "4/news/latest"
        case .sendSMS:
            return "sendSmsService.sendVerifySmsApi"
        case .updateAPi:
            return "versionService.getAppUpdateApi"
        case .productDetail( _):
            return "productService.productDetail"
        case .buyingProduct( _):
            return "productService.productBuy"
        case .serviceList( _):
            return "projectService.projectListPageMix"
        case .serviceDetail( _):
            return "projectService.projectDetail"
        case .buyingService( _):
            return "projectService.projectBuy"
        case .storeList( _):
            return "shopService.shopListPageMix"
        case .storeDetail( _):
            return "shopService.shopDetail"
        case .uploadHeadImage( _):
            return "meiyang_esb/file/user/upload.jhtml"
        case .dealHistory( _):
            return "personalService.dealRecord"
        case .couponList( _):
            return "personalService.getCounponList"
        case .modifyPwd( _):
            return "loginService.modifyPwd"
        case .hadboughtService( _):
            return "personalService.buyedNotUserProjectList"
        case .updateUserInfo( _):
            return "personalService.updateUserInfo"
        case .commentService( _):
            return "personalService.serviceEvaluate"
        case .skinFile( _):
            return "personalService.skinRecord"
        case .usedHistory( _):
            return "personalService.usedProjectList"
        case .getUserInfo( _):
            return "personalService.getUserInfo"
        case .loginByPwd( _):
            return "loginService.login"
        case .loginByMsg( _):
            return "loginService.loginBySendCode"
        case .getMsg( _):
            return "sendSmsService.sendVerifySmsApi"
        case .getImgVerify( _):
            return "meiyang_esb/verCodeServlet.png"
        case .logOut( _):
            return "loginService.logout"
        case .serviceListOfAppoint( _):
            return "userAppointService.getProjectList"
        case .technicianList( _):
            return "userAppointService.getTechnicianList"
        case .appointingService( _):
            return "userAppointService.appointServiceProject"
        case .getDictDesc( _):
            return "dicService.getDicDescApi"
        case .getDictList( _):
            return "dicService.getDicListApi"
        case .wechatLogin( _):
            return "dicService.getDicListApi"
        case .getTicket( _):
            return "wxService.getUserInfoApi"
        case .checkResult( _):
            return "userSkinService.skinCheckHis"
        case .fortest:
            return "mockapi/data"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .easyRequset, .fortest:
            return .get
        default:
            return .post
        }
    }
    

    //    这个是做单元测试模拟的数据，必须要实现，只在单元测试文件中有作用
    var sampleData: Data {
        return "".data(using: String.Encoding.utf8)!
    }

    //    该条请API求的方式,把参数之类的传进来
    var task: Task {
//        return .requestParameters(parameters: nil, encoding: JSONArrayEncoding.default)
        switch self {
        case let .productListPage(para):
            return .requestParameters(parameters:para, encoding: JSONEncoding.default)
        case let .register(email, password):
            return .requestParameters(parameters: ["email": email, "password": password], encoding: JSONEncoding.default)
        case let .sendSMS(para):
            return .requestParameters(parameters: para, encoding: URLEncoding.default)
        case .easyRequset:
            return .requestPlain
        case let .updateAPi(parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case .productDetail(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .buyingProduct(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .serviceList(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .serviceDetail(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .buyingService(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .storeList(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .storeDetail(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .uploadHeadImage(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .dealHistory(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .couponList(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .modifyPwd(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .hadboughtService(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .updateUserInfo(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .commentService(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .skinFile(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .usedHistory(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .getUserInfo(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .loginByPwd(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .loginByMsg(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .getMsg(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .getImgVerify(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .logOut(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .serviceListOfAppoint(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .technicianList(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .appointingService(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .getDictDesc(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .getDictList(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .wechatLogin(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .getTicket(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .checkResult(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

        case .fortest:
            return .requestPlain
        }
        //可选参数https://github.com/Moya/Moya/blob/master/docs_CN/Examples/OptionalParameters.md
//        case .users(let limit):
//        var params: [String: Any] = [:]
//        params["limit"] = limit
//        return .requestParameters(parameters: params, encoding: URLEncoding.default)
    }
 
    
    
    var headers: [String : String]? {
        return ["Content-Type":"application/x-www-form-urlencoded"]
    }
 
}
